home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacWorld Secrets (4th Edition)
/
Mac Secrets CD 4th Ed.toast
/
Apple Advanced Technologies
/
Apple Speech Technologies 1.5
/
PlainTalk Developer Info
/
Speech Recognition Manager SDK
/
SR Sample Code
/
Speakable Items Example
/
Sources
/
SpeakableItems.c
< prev
next >
Wrap
Text File
|
1996-02-22
|
6KB
|
231 lines
/*****************************************************************************/
#include <String.h>
#include <Folders.h>
#include <Files.h>
#include <Script.h>
#include "FinderUtils.h"
#include "SpeakableItems.h"
#define kSIUpdatePeriods 120
#define kSIStrsID 550
#define kSILMName 1
#define kSIFolderName 2
unsigned long gNextSIUpdateTick;
unsigned long gFolderModDate;
short gSIFolderVRefNum;
unsigned long gSIFolderDirID;
OSErr RefetchSIFolderItems (SRRecognitionSystem system, SRLanguageModel siModel);
OSErr GetFileID (StringPtr name, short vRefNum, long parentDirID, long *fileID);
/*****************************************************************************/
OSErr SIInitSpeakableItems (SRRecognitionSystem system, SRLanguageModel *siModel)
{
OSErr status;
long foundDirID;
CInfoPBRec pb;
Str255 name;
SRLanguageModel newModel = 0;
long refCon = kSIModelRefCon;
/* get vRefNum and dirID of speakable items folder */
status = FindFolder(kOnSystemDisk, kAppleMenuFolderType, true,
&gSIFolderVRefNum, &foundDirID);
if (!status) {
GetIndString(name, kSIStrsID, kSIFolderName);
pb.dirInfo.ioCompletion = NULL;
pb.dirInfo.ioNamePtr = name;
pb.dirInfo.ioVRefNum = gSIFolderVRefNum;
pb.dirInfo.ioFDirIndex = 0; /* get info on named item */
pb.dirInfo.ioDrDirID = foundDirID;
status = PBGetCatInfo((CInfoPBPtr)&pb, false);
if (!status)
gSIFolderDirID = pb.dirInfo.ioDrDirID;
}
if (!status) {
/* allow immediate update of LM representing speakable items */
gNextSIUpdateTick = (unsigned long)TickCount() - 1;
gFolderModDate = (unsigned long)pb.dirInfo.ioDrMdDat - 1;
/* create speakable items LM and initialize it */
GetIndString(name, kSIStrsID, kSILMName);
status = SRNewLanguageModel (system, &newModel, name+1, name[0]);
if (!status)
status = SRSetProperty (newModel, kSRRefCon, &refCon, sizeof(refCon));
if (!status) {
status = SIUpdateSpeakableItems (system, newModel);
if (status) {
SRReleaseObject (newModel);
newModel = 0;
}
}
}
*siModel = newModel;
return status;
}
/*****************************************************************************/
/* SIUpdateSpeakableItems changes the given model so it contains the names
of every file in the Speakable Items folder in the Apple Menu Items
folder -- the folder whose identifying information was fetched in the
SIInitSpeakableItems call.
*/
OSErr SIUpdateSpeakableItems (SRRecognitionSystem system, SRLanguageModel siModel)
{
CInfoPBRec pb;
OSErr status = noErr;
// only check every couple seconds
if ((unsigned long)TickCount() > gNextSIUpdateTick) {
// only proceed if folder's modification date changed since last check
pb.dirInfo.ioCompletion = NULL;
pb.dirInfo.ioNamePtr = NULL;
pb.dirInfo.ioVRefNum = gSIFolderVRefNum;
pb.dirInfo.ioFDirIndex = -1; /* get info ioDrDirID directory */
pb.dirInfo.ioDrDirID = gSIFolderDirID;
status = PBGetCatInfo((CInfoPBPtr)&pb, false);
if (!status && (unsigned long)pb.dirInfo.ioDrMdDat != gFolderModDate) {
gFolderModDate = (unsigned long)pb.dirInfo.ioDrMdDat;
status = RefetchSIFolderItems (system, siModel);
}
gNextSIUpdateTick = (unsigned long)TickCount() + kSIUpdatePeriods;
}
return status;
}
/*****************************************************************************/
// Adds name of each item in Speakable Items folder to given language model..
OSErr RefetchSIFolderItems (SRRecognitionSystem system, SRLanguageModel siModel)
{
CInfoPBRec pb;
OSErr status = noErr;
short index;
Str255 name;
long refCon, fileID;
// empty out old LM
status = SREmptyLanguageObject (siModel);
// add name of each item in Speakable Items folder
if (!status) {
// add each speakable item to itemsModel
index = 1;
while (!status) {
pb.dirInfo.ioCompletion = NULL;
pb.dirInfo.ioNamePtr = name;
name[0] = 0;
pb.dirInfo.ioVRefNum = gSIFolderVRefNum;
pb.dirInfo.ioFDirIndex = index;
pb.dirInfo.ioDrDirID = gSIFolderDirID;
status = PBGetCatInfo((CInfoPBPtr)&pb, false);
if (!status)
status = GetFileID (name, gSIFolderVRefNum, gSIFolderDirID, &fileID);
if (!status && name[0] > 0) {
UpperText ((Ptr)(name+1), name[0]);
status = SRAddText (siModel, name+1, name[0], fileID);
}
index++;
}
if (status == fnfErr)
status = noErr;
}
return status;
}
/*****************************************************************************/
OSErr GetFileID (StringPtr name, short vRefNum, long parentDirID, long *fileID)
{
FIDParam block;
FIDParam *pb = █
OSErr status;
pb->ioCompletion = NULL;
pb->ioNamePtr = name;
pb->ioVRefNum = vRefNum;
pb->ioSrcDirID = parentDirID;
status = PBCreateFileIDRefSync ((HParmBlkPtr)pb);
if (status == fidExists) status = noErr;
if (!status)
*fileID = pb->ioFileID;
else
*fileID = 0;
return status;
}
/*****************************************************************************/
OSErr SIProcessResult (SRLanguageModel siResModel)
{
OSErr status;
SRPhrase phrase = 0;
long fileID;
Size len = sizeof(fileID);
FSSpec itemFSSpec;
CInfoPBRec pb;
SRLanguageModel itemsLM;
SRLanguageObject subLM;
SRPath path;
long numItems;
FIDParam block;
FIDParam *pb2 = █
// get subelement of given language model (siResModel).
// since siResModel parallels the model we built in RefetchSIFolderItems()
// above, the subelement will be a copy of one of the phrases
// that was added to that model. Each of those phrases is the name
// of an item in the Speakable Items folder. And the refCon of each
// phrase gives the fileID of the corresponding file.
status = SRGetIndexedItem (siResModel, &phrase, 0);
if (!status && phrase) {
// get fileID, stored as refCon
status = SRGetProperty (phrase, kSRRefCon, &fileID, &len);
// resolve file id to get original name
if (!status) {
pb2->ioCompletion = NULL;
pb2->ioNamePtr = itemFSSpec.name;
pb2->ioVRefNum = gSIFolderVRefNum;
pb2->ioFileID = fileID;
status = PBResolveFileIDRef((HParmBlkPtr)pb2, false);
if (!status) {
itemFSSpec.vRefNum = gSIFolderVRefNum;
itemFSSpec.parID = gSIFolderDirID;
}
}
// open file
if (!status)
status = FUOpenFinderItem(&itemFSSpec, false);
// release extra reference now that we're done with it
SRReleaseObject (phrase);
}
return status;
}
/*****************************************************************************/